import java.lang.reflect.Array;
import java.util.Arrays;

public class Test {

    public static void main(String[] args){
        


/*
        int[] arr = new int[]{1,2,3,4,5,6,7,8,9,10};

        System.out.println(Arrays.binarySearch(arr, 9));//在数组范围内查找18的下标

        System.out.println(Arrays.binarySearch(arr,0,8, 6));//在数组范围[0,8)内查找18的下标
*/
        /*int[] arr1 = new int[]{1,5,9,11,4,18,6,66,56};
        int[] arr2 = new int[10];
        int[] arr3 = new int[]{8,8,8,8,8,8,8,8,8,8,8,8};


        System.out.println("拷贝前"+Arrays.toString(arr2));//拷贝前
        arr2 = Arrays.copyOf(arr1,arr1.length);//将arr1中的arr1.lenght个元素拷贝
        System.out.println("第一次拷贝后"+Arrays.toString(arr2));//第一次拷贝后


        arr2 = Arrays.copyOfRange(arr3,0,5);//将arr3中的[0,5)范围内元素拷贝
        System.out.println("第二次拷贝后"+Arrays.toString(arr2));//第二次拷贝后

        arr2 = Arrays.copyOf(arr2,20);//也可以实现扩容，没有分配数据的地方会拿0填充
        System.out.println("第二次拷贝后"+Arrays.toString(arr2));//第三次填充后*/



/*

*/


/*
        int[] arr2 = new int[]{1,5,9,5,4,18,6,66,56};
        int[] arr3 = new int[]{1,2,3,4,5,6,7,8,9,10};

        System.out.println(Arrays.equals(arr1, arr2));//比较arr1与arr2并且打印
        System.out.println(Arrays.equals(arr1, arr3));//比较arr1与arr3并且打印
*/

        //System.out.println(Arrays.toString(arr));
/*
        System.out.println("arr排序前：" + Arrays.toString(arr));  //打印

        Arrays.sort(arr);//排序arr整个数组
        System.out.println("arr第一次排序后：" + Arrays.toString(arr));  //打印

        Arrays.sort(arr,0,5);//将数组arr下标为[0,3)范围内排序
        System.out.println("arr第二次排序后：" + Arrays.toString(arr));  //打印
*/

/*
        System.out.println("arr填充前：" + Arrays.toString(arr));  //打印

        Arrays.fill(arr,0,3,2); //将数组arr下标为[0,3)范围内填充
        System.out.println("arr第一次填充：" + Arrays.toString(arr));  //打印

        Arrays.fill(arr,1);   //将数组arr每个元素用1填充
        System.out.println("arr第二次填充：" + Arrays.toString(arr));  //打印
*/

        /*int[][] arr1 = new int[3][];
        int[][] arr2 = new int[][]{{1,2},{3,4,5},{6}};
        int[][] arr3 = {{1},{2,3},{4,5,6}};
*/

/*
            int a = 10;
            int b = 20;
            int[] arr = new int[]{1,2,3};

*/


        //int[] arr = new int[]{1,2,3,4,5,6,7,8,9,10};
        //reverse(arr);
        //System.out.println(Arrays.toString(arr));


        //int[] arr = new int[]{3,2,3};
        //System.out.println(find(arr));
        //System.out.println(nums(arr));
        //System.out.println(Arrays.toString(twoSum(arr, 12)));

       // System.out.println(Arrays.toString(bubble(arr)));

        //System.out.println(Arrays.toString(transform(arr)));
        //int[] arr = new int[]{1,2,38,4,15,6,78,8,9,10};
        //Arrays.parallelSort(arr);
        //System.out.println(Arrays.toString(bubbleSort(arr)));
        //System.out.println(Arrays.toString(arr));
        //System.out.println(find(arr, 78));
        //System.out.println(Arrays.binarySearch(arr, 78));

        /*int[] arr = new int[]{1,2,3,4,5,6,7,8,9,10};
        System.out.println(avg(arr));*/

        /*int[] arr1 = {1,2,3,4,5};
        int[] arr2 = Arrays.copyOf(arr1,arr1.length);
        int[] arr3 = Arrays.copyOfRange(arr1,0,8);
        System.out.println(Arrays.toString(arr2));
        System.out.println(Arrays.toString(arr3));*/



        /*int[] arr1 = {1,2,3,4,5};
        String newArr = Arrays.toString(arr1);
        System.out.println(newArr);
*/

    }
    //给定一个大小为 n 的数组，找到其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。

    public static void reverse(int[] arr){
        int left = 0;
        int right = arr.length - 1;
        while(left < right){
            int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
            left++;
            right--;
        }
    }

    public static int find(int[] arr){
        int n = arr.length;
        for (int i = 0; i < n ;i++) {
            int count = 0;
            for (int j = 0; j < n; j++) {
                if(arr[i] == arr[j]){
                    count++;
                }
            }
            if(count  > n / 2 ){
                return arr[i];
            }
        }
        return -1;
    }

    public static boolean nums(int[] arr){
        for (int i = 0; i < arr.length - 2; i++) {
            if(arr[i] % 2 != 0 && arr[i + 1] % 2 != 0 && arr[i + 2] % 2 != 0){
                return true;
            }
        }
        return false;
    }


    public static void singleNumber(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            int tmp = 0;
            for (int j = 0; j < arr.length; j++) {
                if(arr[i] == arr[j] ){
                    tmp++;
                }
            }
            if(tmp == 1){
                System.out.println(arr[i]);
            }
        }
    }
    public static int[] twoSum(int[] arr,int key){
        int[] newArr = new int[2];
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if(arr[i] + arr[j] == key){
                    newArr[0] = i;
                    newArr[1] = j;
                    return newArr;
                }
            }
        }
        return newArr;
    }

    public static int[] bubble(int[] arr){
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if(arr[j] % 2 == 0){
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                }
            }
        }
        return arr;
    }

    public static int[]  transform(int[] arr){
        for(int i = 0;i < arr.length;i++){
            arr[i] *= 2;
        }
        return arr;
    }

    public static int[] bubbleSort(int[] arr){
        boolean ret = true;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - i -1; j++) {
                if(arr[j] > arr[j + 1]) {
                    int tmp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = tmp;
                    ret = false;
                }
            }
            if(ret){
                return arr;
            }
        }
        return arr;
    }
    public static int find(int[] arr,int key){
        int right = arr.length - 1;
        int left = 0;
        while(left <= right){
            int mid = (right + left) >>> 1;
            if(arr[mid] > key){
                right = mid - 1;
            } else if(arr[mid] < key){
                left = mid + 1;
            }else {
                return mid + 1;
            }
        }
    return -1;
    }
    public static int[] copyOf(int[] arr){
        int[] arr1 = new int[arr.length];
        for(int i = 0;i < arr.length;i++){
            arr1[i] = arr[i];
        }
        return arr1;
    }

    public static double avg(int[] arr){
        int sum = 0;
        for (int x:arr) {
            sum += x;
        }
        return (double)sum / (double) arr.length;
    }
}























